﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ControlLibary
{
    public class ProgressColumn : DataGridViewColumn
    {
        public ProgressColumn()
            : base(new ProgressCell())
        {
            ReadOnly = true;
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(ProgressCell)))
                {
                    throw new InvalidCastException("Must be a ProgressCell");
                }
                base.CellTemplate = value;
            }
        }
    }

    public class ProgressCell : DataGridViewTextBoxCell
    {

        private double minValue = 0;

        public double MinValue
        {
            get { return minValue; }
            set { minValue = value; }
        }

        private double maxValue = 100;

        public double MaxValue
        {
            get { return maxValue; }
            set { maxValue = value; }
        }

        public override Type ValueType
        {
            get
            {
                return typeof(double);
            }
        }

        private Color progressColor = Color.FromArgb(163, 207, 76);

        public Color ProgressColor
        {
            get { return progressColor; }
            set { progressColor = value; }
        }

        private Color progressBorderColor = Color.FromArgb(209, 209, 209);
        public Color ProgressBorderColor
        {
            get { return progressBorderColor; }
            set { progressBorderColor = value; }
        }

        private Color progressTextColor = Color.FromArgb(45, 45, 48);
        public Color ProgressTextColor
        {
            get { return progressTextColor; }
            set { progressTextColor = value; }
        }

        protected override void Paint(
            System.Drawing.Graphics graphics,
            System.Drawing.Rectangle clipBounds,
            System.Drawing.Rectangle cellBounds,
            int rowIndex,
            DataGridViewElementStates cellState,
            object value, object formattedValue,
            string errorText, DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
        {
            using (SolidBrush sb = new SolidBrush(this.Selected ? this.DataGridView.DefaultCellStyle.SelectionBackColor : this.DataGridView.DefaultCellStyle.BackColor))
            {
                graphics.FillRectangle(sb, cellBounds/*new Rectangle(cellBounds.X, cellBounds.Y, cellBounds.Width - 1, cellBounds.Height - 1)*/);
            }

            using (Pen p = new Pen(this.DataGridView.GridColor, 1f))
            {
                graphics.DrawRectangle(p, new Rectangle(cellBounds.X - 1, cellBounds.Y - 1, cellBounds.Width, cellBounds.Height));
            }

            float height = (cellBounds.Height + 5) / 2;

            float width = cellBounds.Width - 12;

            RectangleF rec = new RectangleF(cellBounds.X + 5, cellBounds.Y + (cellBounds.Height - height) / 2, width, height);



            double v = 0;

            if (value is double)
            {
                v = (double)value;
            }
            else if (value is string)
            {
                double.TryParse((string)value, out v);
            }
            else if (value is int)
            {
                v = (double)((int)value);
            }

            string text = string.Format("{0:F2}%", (double)(v / maxValue) * 100.0);

            SizeF sizeF = graphics.MeasureString(text, this.DataGridView.Font, (int)rec.Width);

            double temp = (width / maxValue) * v;


            //LinearGradientBrush linGrBrush = new LinearGradientBrush(new Point(0, 0), new Point(180, 0), Color.Black, Color.White);
            LinearGradientBrush linGrBrush1 = new LinearGradientBrush(new PointF(0, 0), new PointF(rec.Width, 0), progressColor, progressColor);

            using (linGrBrush1)
            {
                RectangleF _rec = new RectangleF(cellBounds.X + 6, cellBounds.Y + (cellBounds.Height - height) / 2, (float)temp - 1, height - 1.2f);

                graphics.FillRectangle(linGrBrush1, _rec);
            }

            using (Pen p = new Pen(progressBorderColor, 1f))
            {
                graphics.DrawRectangle(p, rec.X, rec.Y, rec.Width, rec.Height);
            }

            using (SolidBrush foreBru = new SolidBrush(progressTextColor))
            {
                graphics.DrawString(text, this.DataGridView.Font, foreBru, rec.Left + (rec.Width - sizeF.Width) / 2, rec.Top + (rec.Height - sizeF.Height) / 2 + 1);
            }
        }
    }
}
